package guoanmaker.operator.business.service;

import guoanmaker.operator.business.model.OperatorHeadline;
import guoanmaker.operator.business.repository.OperatorHeadlineRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

import javax.transaction.Transactional;

/**
 *  
 *
 * @ClassName: OperatorHeadlineService 
 * @Description: OperatorHeadlineService 
 * @author: huZhaoyu 
 * @date 2017/6/28 下午2:56 
 */
@Service
public class OperatorHeadlineService implements OperatorHeadlineServiceInterface {
    private final OperatorHeadlineRepository operatorHeadlineRepository;

    @Override
    public Integer countAllById() {
        return operatorHeadlineRepository.countAllByIdIsNotNull();
    }

    @Override
    public Page<OperatorHeadline> getHeadlineByPageOrderByWeight(Integer page, Integer size) {
        return operatorHeadlineRepository.findAll(new PageRequest(page, size, new Sort(Sort.Direction.DESC, "weight")));
    }

    @Autowired
    public OperatorHeadlineService(OperatorHeadlineRepository operatorHeadlineRepository) {
        this.operatorHeadlineRepository = operatorHeadlineRepository;
    }

    @Override
    @Transactional
    public OperatorHeadline saveHeadline(OperatorHeadline operatorHeadline) {
        return operatorHeadlineRepository.save(operatorHeadline);
    }

    @Override
    public List<OperatorHeadline> getAllHeadline(String cityId) {
        return operatorHeadlineRepository.findBycityCode(cityId);
    }

    @Override
    public Page<OperatorHeadline> getAllHeadlineByPage(Integer page, Integer size) {
        return operatorHeadlineRepository.findAll(new PageRequest(page, size));
    }

    @Override
    @Transactional
    public void deleteHeadlineById(String id) {
        operatorHeadlineRepository.delete(id);
    }

    @Override
    public OperatorHeadline getHeadlineById(String id) {
        return operatorHeadlineRepository.findOne(id);
    }
}
